package com.smile.service.handler;

import com.alibaba.fastjson2.JSONObject;
import com.smile.service.config.mqtt.constant.MethodConstant;
import com.smile.service.config.mqtt.handler.AbstractConfigHandler;
import com.smile.service.entity.config.DeviceAppConfig;
import com.smile.service.entity.response.DeviceConfigResponse;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DefaultConfigHandler extends AbstractConfigHandler {

    @Value("${cloud.appId}")
    private String appId;

    @Value("${cloud.appKey}")
    private String appKey;

    @Value("${cloud.appLicense}")
    private String appLicense;


    @Value("${spring.mqtt.server-url}")
    private String brokerUrl;

    @Value("${spring.mqtt.username}")
    private String username;

    @Value("${spring.mqtt.password}")
    private String password;

    @Override
    public Boolean process(String data, String tid, String bid, String gateway) throws MqttException {

        System.out.println(data);


        DeviceConfigResponse<DeviceAppConfig> deviceConfigResponse = new DeviceConfigResponse<>();


        DeviceAppConfig deviceAppConfig = new DeviceAppConfig();
        deviceAppConfig.setAppId("144844");
        deviceAppConfig.setAppKey("db858df7ef2c2de9b566414a0d54de9");
        deviceAppConfig.setAppLicense("BVTjqXIO2t8OlaIpK2XEKohfC0eBIQx2SIM6o0qUS6wYe9mYG3M163Gq1DeJzrbvlSKZ/sFKlr7eTO6u9OFKBkZfpbJQy/yrFDNaIKwM+hSS1OFjCLibdi2jxjlnDpW+JnHl0SplUOFbErCOTVYYAckEuMXf7wqHQSofSYrmgpY=");
        deviceAppConfig.setNtpServerPort(123);
        deviceAppConfig.setNtpServerHost("192.168.0.80");
        deviceConfigResponse.setData(deviceAppConfig);

        //{"tid":"b098d85b-b9a3-4a89-b4c6-afb392bd4a27","bid":"4f762225-b612-4395-9cbc-1b9cb4637cbb","timestamp":1750744078601,"method":"config","data":{"config_type":"json","config_scope":"product"},"gateway":"8UUXN2800A00HX"}

        deviceConfigResponse.setBid(bid);
        deviceConfigResponse.setTid(tid);
        deviceConfigResponse.setGateway(gateway);
        deviceConfigResponse.setTimestamp(System.currentTimeMillis());
        deviceConfigResponse.setMethod(MethodConstant.CONFIG);

        log.info("DefaultConfigHandler {}", JSONObject.toJSONString(deviceConfigResponse));

        //        deviceAppConfig.setNtpServerHost();
//        deviceAppConfig.setNtpServerPort();

        MqttMessage message = new MqttMessage();
        message.setQos(0);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(deviceConfigResponse).getBytes());
        MqttClient mqttClient = new MqttClient(brokerUrl, "feisuan-mqtt-producer-client", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName(username);
        connOpts.setPassword(password.toCharArray());
        mqttClient.connect(connOpts);

//        mqttClient.publish("thing/product/" + gateway + "/requests_reply", message);
        mqttClient.publish("thing/product/" + gateway + "/requests_reply", message);
        return Boolean.TRUE;
    }


}
